package my.jieh.tips.io.file;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File;

/**
 * <p>Title: Core Java Technologies Tech Tips</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: Blueimp</p>
 * @author jieh
 * @version 1.0
 */

public class chooser {
  public static void main(String args[]) {
    JFrame frame = new JFrame("File Chooser demo");
    frame.addWindowListener(new WindowAdapter() {
      public void windowClosing(WindowEvent e) {
        System.exit(0);
      }
    });

// label to attach image icon to
    final JLabel label =
        new JLabel("", SwingConstants.CENTER);

    JPanel panel1 = new JPanel();
// start filechooser in current directory
    String cwd = System.getProperty("user.dir");
    final JFileChooser fc = new JFileChooser(cwd);
    fc.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
// set label's icon to the current image
        String state =
            (String)e.getActionCommand();
        if (!state.equals(
                          JFileChooser.APPROVE_SELECTION))
          return;
        File f = fc.getSelectedFile();
        if (f == null || !f.isFile())
          return;
        ImageIcon icon =
            new ImageIcon(f.getPath());
        label.setIcon(icon);
      }
    });
// set the file filter for the filechooser
    fc.setFileFilter(new ImageFileFilter());
    panel1.add(fc);

    JPanel panel2 = new JPanel();
// max size of label whose icon displays image
    label.setPreferredSize(new Dimension(500, 300));
    panel2.add(label);

    frame.getContentPane().add("North", panel1);
    frame.getContentPane().add("South", panel2);
    frame.pack();
    frame.setVisible(true);
  }
}